package kyssion.leetcode.num201_250;

public class code209_最长最小子数组 {

    public static void main(String[] args) {
        System.out.println(new code209_最长最小子数组().minSubArrayLen(
                6, new int[]{
                        2, 3, 1, 2, 4, 3,2
                }
        ));
    }

    /**
     * O(n)
     * @param s
     * @param nums
     * @return
     */
    public int minSubArrayLen(int s, int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int start = 0;
        int end = 0;
        int all = 0;
        int time = 0;
        int allTime = 0;
        while (end < nums.length) {
            all += nums[end];
            if (all >= s) {
                time = end - start + 1;
            }
            for (int a = start; a < end; a++) {
                if (all - nums[a] >= s) {
                    start++;
                    time--;
                    all -= nums[a];
                } else {
                    break;
                }
            }
            allTime = allTime == 0 ? time : Math.min(allTime, time);
            end++;
        }
        return allTime;
    }


}
